import{reqGetCode ,reqUserRegister,reqUserLogin,reqUserInfo,reqLogout} from'../api'
import { setToken, getToken,removeToken} from "@/utils/token";

const state={
    code:'',
    token:getToken(),
    userInfo:{}
};
const mutations={
    GETCODE(state,code){
        state.code = code
    },
    GETUSERINFO(state,userInfo){
        state.userInfo = userInfo;
    },
    USERLOGIN(state, token) {
        state.token = token;
      },
    CLEAR(state){
        state.userInfo={};
        state.token=''
        removeToken()
    }
};
const actions={
    async getCode({commit},phone){
       let result = await reqGetCode(phone);
       if(result.code == 200){
        commit("GETCODE", result.data);
        return 'ok'
       }else{
           return Promise.reject(new Error('faile'))
       }
    },
    async userRegister({commit},user){
        let result = await reqUserRegister(user);
        if (result.code == 200) {
            return "ok";
          } else {
            return Promise.reject(new Error("faile"));
          }
    },
    async userLogin({commit},user){
        let result = await reqUserLogin(user);
        if (result.code == 200) {
            //用户已经登录成功且获取到token
            commit("USERLOGIN", result.data.token);
            setToken(result.data.token)
            return 'ok'
        }else{
            return Promise.reject(new Error("faile"));
        }
    },
    async getUserInfo({commit}){
        let result = await reqUserInfo();
        if (result.code == 200) {
          //提交用户信息
          commit("GETUSERINFO", result.data);
          return 'ok';
        }else{
          return Promise.reject(new Error('faile'));
        }
    },
    async userLogout({commit}){
        let result = await reqLogout();
        if (result.code == 200) {
            //提交用户信息
            commit("CLEAR");
            return 'ok';
          }else{
            return Promise.reject(new Error('faile'));
          }
    }
};
const getters={};
export default{
    state,
    mutations,
    actions,
    getters
}